import { http } from '../http'

const store = {
  state: {
    userInfo: null,
  },
  getType(){
    if(this.state.userInfo){
      return this.state.userInfo.type
    }
    this.loadUserInfo().then(res => {
      this.state.userInfo = res
      return this.getType()
    })
  },
  getUserInfo() {
    return this.state.userInfo
  },
  loadUserInfo() {
    return new Promise(resolve => {
      http.get('/userinfo').then(res => {
        if (res.code === 0) {
          resolve(res.data)
        } else {
          console.info('Server exception')
        }
      })
    })
  }
};
export default store
